/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package consumo.face.cadastro;

import consumo.entity.Concessionaria;
import consumo.face.BaseCDIBean;
import consumo.session.ConcessionariaSession;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.inject.Inject;

/**
 *
 * @author Carlos Taschetto
 */

@ManagedBean
@ViewScoped
public class ConceFace extends BaseCDIBean<Concessionaria> {

    private static final long serialVersionUID = 1L;
    
    @Inject
    private ConcessionariaSession bean;
    
    private List<Concessionaria> listaConce;
    private boolean isNew;


    @PostConstruct
    protected void init(){
        System.out.println("init();");
        atribuiEstadoInicial();
    }

    private void atribuiEstadoInicial(){
        listaConce = null;
        setSelectedBean(new Concessionaria());
        System.out.println("estado inicial!!!!!");
    }

    
    /**
     * inicializa uma nova concessionária a ser gravada
     */
    public void doCreateConce(){
        System.out.println("Entrou em createConce;");
        setIsNew(true);
        Concessionaria novo = new Concessionaria();
   
        setSelectedBean(novo);
        System.out.println(getSelectedBean());
    }
    /**
     * Salva a concessionária
     */
    private void doFinishCreateConce(){
        bean.saveConcessionaria(getSelectedBean());
        listaConce = bean.getAllConcessionarias();
    }

    /**
     * Edita uma concessionaria
     */
    private void doEditConce(){
        bean.setConcessionaria(getSelectedBean());
        listaConce = bean.getAllConcessionarias();
    }
    /**
     * Verifica se é uma nova inserção ou uma alteração
     */
    public void doSaveConcessionaria(){
        System.out.println("Entrou doSave: "+isNew);
        System.out.println("Entrou doSave: "+getSelectedBean());
        System.out.println("Lista: "+listaConce);
        System.out.println("getLista -->"+getListaConce());
        if(isIsNew())
            doFinishCreateConce();
        else doEditConce();

        //setSelectedBean(null);
    }


    public List<Concessionaria> getListaConce() {
        if(listaConce==null){
            System.out.println("Iniciou: "+listaConce);
            listaConce = bean.getAllConcessionarias();
        }
        System.out.println("Lista Conce: "+listaConce);
        return listaConce;
    }

    public void setListaConce(List<Concessionaria> listaConce) {
        this.listaConce = listaConce;
    }

    public void doRemoveConce(){
        bean.removeConcessionaria(getSelectedBean());
        System.out.println("doRemove");
        setListaConce(bean.getAllConcessionarias());
    }

    public boolean isIsNew() {
        return isNew;
    }

    public void setIsNew(boolean isNew) {
        this.isNew = isNew;
    }

    public void resetIsNew(){
        this.isNew = false;
    }
}
